<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="java.util.*"%>
<%@ page import="goods.*"%>
<%@ page import="review.*"%>
<!DOCTYPE html PUBLIC "-//W3C//Dtd HTML 4.01 transitional//EN" "http://www.w3.org/tr/html4/loose.dtd">

<!-- 상단프레임 -->
<%@ include file="/Dmyna/head.jsp"%>

<!-- 게시물 수 -->
<%!static int PAGE_SIZE = 20;%>

<!--  -->
<%
	String mcd = request.getParameter("MCD");	
	String pageNum = request.getParameter("page");
	if (pageNum == null) pageNum = "1";
	int currentPage = Integer.parseInt(pageNum);
	GoodsManager manager = GoodsManager.getInstance();
	int count = manager.count(mcd);
	int totalPageCount = 0;
	int startrow = 0, endRow = 0;
	if (count > 0) {
		totalPageCount = count / PAGE_SIZE;
		if (count % PAGE_SIZE > 0) totalPageCount++;
		startrow = (currentPage - 1) * PAGE_SIZE;
		endRow = currentPage * PAGE_SIZE;
	}
	List<GoodsItem> list = manager.selectList(startrow, PAGE_SIZE, mcd);
	
	int blankCnt = 4 - (count % 4);
	if(blankCnt == 4) blankCnt=0;
	
/* 	String gs_code = request.getParameter("gs_code");
	ReviewWritingManager remanager = ReviewWritingManager.getInstance();
	ReviewWriting review_count = remanager.select3(gs_code); */
	
	
%>

<!--  -->
<c:set var="list" value="<%=list%>" />
<c:set var="mcd" value="<%=mcd%>" />
<c:set var="dbCnt" value="<%=count%>" />
<c:set var="blankCnt" value="<%=blankCnt%>" />
<!--  -->

<!-- 상단제목 -->
<table width="900" align=center cellpadding=0 cellspacing=0>
	<tr>
		<td>
			<div
				style="border:1px solid #ddd; height:34px; background:url(${WEB_ROOT }/img/bbs/title_bg.gif) repeat-x;">
				<div style="font-weight: bold; font-size: 14px; margin: 7px 0 0 10px;" align="center">
					::&nbsp;&nbsp;${MENU_NAME }&nbsp;&nbsp;::</div>
			</div>
		</td>
	</tr>
</table>
<p>
<!--  -->

<TABLE border="0" width="900" align="center" cellspacing="0">

	<c:if test="${empty list }">
		<tr>
			<td bgcolor="#f0f0f0" colspan="5" height="100" align="center">등록된상품이 없습니다.</td>
		</tr>
	</c:if>

		<c:if test="${!empty list }">
			<c:set var="goodsCnt" value="<%=startrow%>" />
			<c:forEach var="rowcnt" begin="0" end="4">
				<tr>
					<c:forEach var="goods" items="${list}" begin="${ (rowcnt*4) }" end="${(rowcnt*4) + 3 }">
						<td width="25%">
							<TABLE width="100%" height="190" border="0" cellpadding="0"cellspacing="0">

								<!-- 리스트 시작 -->
								<tr>
									<td height="100%" align="center"><a class="img" href="javascript:goView('${goods.gs_code }')"> 
									<img src="upload/${goods.gs_img }" width="170" border='1'>
									</a></td>
								</tr>
								<tr height="20"><td class="subject" align="center">
								<c:if test="${goods.gs_view_cnt >= 50 }">
								<img src="${WEB_ROOT}/img/goods/goods_hititem.gif" />
								</c:if>
								<c:if test="${goods.gs_view_cnt < 50 && goods.gs_view_cnt > 20 }">
								<img src="${WEB_ROOT}/img/goods/goods_coolitem.gif" />
								</c:if>
								<c:if test="${goods.gs_org_price > goods.gs_sale_price && goods.gs_org_price - goods.gs_sale_price >= 10000 }">
								<img src="${WEB_ROOT}/img/goods/goods_special.gif" />
								</c:if>
								<%-- <img src="${WEB_ROOT}/img/goods/goods_order.gif" /> --%></td>
								</tr>
								<tr height="30">
									<%-- <td class="subject" align="center">상품명 : ${goods.gs_name }</td> --%>
									<td class="subject" align="center"><a class="img" href="javascript:goView('${goods.gs_code }')"><font style="color: #778899; font-weight:bold">${goods.gs_name }</font></a></td>
								</tr>
								<tr height="30">
									<%-- <td class="price" align="center">가격 : ${goods.gs_org_price }원</td> --%>
									<c:choose>
									<c:when test="${goods.gs_org_price == goods.gs_sale_price }">
									<td class="price" align="center"><font style="font-weight:bold"  ><fmt:formatNumber value="${goods.gs_org_price }" type="number"/>원</font> </td>
									</c:when>
									<c:otherwise>
									<td class="price" align="center"><strike><fmt:formatNumber value="${goods.gs_org_price }" type="number"/>원</strike> &nbsp;<font style="font-weight:bold" > <fmt:formatNumber value="${goods.gs_sale_price }" type="number"/>원</font></td>
									</c:otherwise>
									</c:choose>
								</tr>
								<tr>
									<td colspan=2 height=1 bgcolor=#e7e7e7></td>
								</tr>
								<tr>
									<td><br /> <br /></td>
								</tr>
								<!-- 리스트 종료 -->

							</TABLE>
						</td>
						<c:set var="goodsCnt" value="${ (goodsCnt+1) }" />
						<c:if test="${dbCnt == goodsCnt }">
						
						<c:forEach var="blankcol"  begin="1" end="${blankCnt }">
						<TD width="25%">
							<TABLE width="100%" height="190" border="0" cellpadding="0"cellspacing="0">
								<TR>
									<TD height="100%"><a class="" href=""> <img src="${WEB_ROOT}/img/goods/blank.png" width="221" border='0'>
									</a></TD>
								</TR>
								<tr height="20">
									<td class="subject" align="center">&nbsp;</td>
								</tr>
								<tr height="30">
									<td class="subject" align="center">&nbsp;</td>
								</tr>
								<tr height="30">
									<td class="price" align="center">&nbsp;</td>
								</tr>
								<tr>
									<td colspan=2 height=1 bgcolor=#e7e7e7></td>
								</tr>
								<tr>
									<td><br /> <br /></td>
								</tr>
							</TABLE>
						</TD>
						</c:forEach>
						
					</c:if>
					</c:forEach>
				</tr>
			</c:forEach>
		</c:if>
	<tr>
		<td colspan='5' height=25 align=center>
			<div class="board_page">

				<!--페이지 번호 시작-->
				<c:set var="count" value="<%=Integer.toString(count)%>" />
				<c:set var="PAGE_SIZE" value="<%=Integer.toString(PAGE_SIZE)%>" />
				<c:set var="currentPage" value="<%=Integer.toString(currentPage)%>" />
				<c:if test="${count > 0 }">

					<c:set var="pageCount"
						value="${count / PAGE_SIZE + (count % PAGE_SIZE == 0 ? 0 : 1) }" />
					<c:set var="startPage"
						value="${currentPage - (currentPage % 10 == 0 ? 10:(currentPage % 10)) + 1 }" />
					<c:set var="endPage" value="${startPage + 9 }" />
					<c:if test="${endPage > pageCount }">
						<c:set var="endPage" value="${pageCount }" />
					</c:if>

					<!-- 처음버튼 -->
					<c:if test="${startPage > 1  }">
						<a href="javascript:goPage(${startPage})"> <img
							src='${WEB_ROOT }/img/bbs/page_begin.gif' border='0'
							align='absmiddle' title='처음'>&nbsp;
						</a>
					</c:if>

					<!-- 이전버튼 -->
					<c:if test="${startPage > 10  }">
						<a href="javascript:goPage(${startPage-10 })"> <img
							src='${WEB_ROOT }/img/bbs/page_prev.gif' border='0'
							align='absmiddle' title='이전'>&nbsp;
						</a>
					</c:if>

					<!-- 숫자표시 -->
					<c:forEach var="pageNo" begin="${startPage }" end="${endPage }">
						<c:if test="${currentPage == pageNo }">
							<font size="4">
						</c:if>
						<a href="javascript:goPage(${pageNo })">${pageNo }&nbsp;</a>
						<c:if test="${currentPage == pageNo }">
							</font>
						</c:if>
					</c:forEach>

					<!-- 다음버튼 -->
					<c:if test="${endPage < pageCount  }">
						<a href="javascript:goPage(${startPage+10 })"> <img
							src='${WEB_ROOT }/img/bbs/page_next.gif' border='0'
							align='absmiddle' title='다음'>&nbsp;
						</a>
					</c:if>

					<!-- 맨끝버튼 -->
					<c:if test="${startPage > 1  }">
						<a
							href="javascript:goPage(<fmt:formatNumber value="${endPage}" pattern="#"/>)">
							<img src='${WEB_ROOT }/img/bbs/page_end.gif' border='0'
							align='absmiddle' title='맨끝'>
						</a>
					</c:if>
				</c:if>
				<!--페이지 번호 종료-->

			</div>
		</td>
	</tr>
	<tr>
		<!-- 전체게시물수 표시 -->
		<td colspan="5" height=25 align="left">${dbCnt } 개의 상품이 있습니다.</td>
	</tr>
</TABLE>

<!-- 하단프레임 -->
<%@ include file="/Dmyna/tail.jsp"%>

<!--  -->
<script language="JavaScript">
	function goPage(pageNo) {
		document.move.action = "${WEB_ROOT }/goods/goodsList.jsp";
		document.move.page.value = pageNo;
		document.move.submit();
	}

	function goView(gscode) {
		document.move.action = "${WEB_ROOT }/goods/goodsView.jsp";
		document.move.GS_CODE.value = gscode;
		document.move.submit();
	}
</script>

<!--  -->
<form name="move" method="post">
	<input type="hidden" name="GS_CODE" value="" /> 
	<input type="hidden" name="page" value="" />
	<input type="hidden" name="MCD" value="${mcd }" />
</form>